/**
 * 实体
 */
Ext.define('cc.self.view.SelfJh', {
	extend: 'Ext.form.Panel',
	requires: ['cc.self.util.SelfService'],
	autoScroll: true,
	//是否显示编辑器
	enableEdt: true,
	
	defaults: {
		xtype: 'textfield',
		anchor: '50%',
		labelAlign: 'right',
		cls: 'cc_float_left',
		margin: '10 10 0 10'
	},

	viewModel: {
		data: {
			rec: null
		},
		formulas: {
			jhmoney: function(get) {
				var jh = Ext.Number.toFixed(get('rec.sh_money') * get('rec.rate'), 2);
				this.get('rec').set('jh_money', jh);
				return jh;
			}
		}
	},

	initComponent: function() {
		var me = this;
		
		me.items = [{
			xtype: 'datefield',
			fieldLabel: '结汇日期',
			format: 'Y-m-d',
			bind: '{rec.jh_date}'
		}, {
			xtype: me.enableEdt ? 'numberfield' : 'displayfield',
			fieldLabel: '收汇金额USD',
			cls: '',
			bind: '{rec.sh_money}'
		}, {
			xtype: me.enableEdt ? 'numberfield' : 'displayfield',
			decimalPrecision: 4,
			fieldLabel: '汇率',
			bind: '{rec.rate}'
		}, {
			xtype: me.enableEdt ? 'numberfield' : 'displayfield',
			readOnly:true,
			fieldCls: 'cc_readonly',
			fieldLabel: '结汇金额RMB',
			cls: '',
			bind: '{jhmoney}'
		}, {
			xtype: me.enableEdt ? 'numberfield' : 'displayfield',
			fieldLabel: '手续费',
			bind: '{rec.sxf}'
		}, {
			xtype: me.enableEdt ? 'numberfield' : 'displayfield',
			fieldLabel: '兑换汇率',
			decimalPrecision:4,
			cls: '',
			bind: '{rec.dh_rate}'
		}, {
			xtype: me.enableEdt ? 'numberfield' : 'displayfield',
			fieldLabel: '金额RMB',
			bind: '{rec.jh_money_rmb}'
		}];

		me.callParent(arguments);
	},

	save: function(cb) {
		var me = this;
		var rec = me.getViewModel().getData().rec;
		var d = rec.getData();
		if(rec.get('id') > 0){
			//update
			CC.Self.updateSelfJh({data: Ext.encode(d)}, function(){
				rec.commit();
				if(cb){
					cb(rec);
				}
			});
		}else{
			//create
			CC.Self.createSelfJh({data: Ext.encode(d)}, function(data){
				rec.set('id', data.id);
				rec.commit();
				if(cb){
					cb(rec, true);
				}
			});
		}
	},
	
	loadEntity: function(key){
		var me = this;
		CC.Self.getSelfJh(key, function(data){
			me.getViewModel().set('rec', Ext.create('cc.self.model.SelfJh', data));
		});
	}
});
